const Mock = require('mockjs')
// const subJectArr = [
//   {
//     subjectId: 1,
//     subjectName: '测试学科'
//   },
//   {
//     subjectId: 2,
//     subjectName: 'Python+人工智能'
//   },
//   {
//     subjectId: 3,
//     subjectName: '云计算'
//   },
//   {
//     subjectId: 4,
//     subjectName: '全链路UI设计'
//   },
//   {
//     subjectId: 5,
//     subjectName: 'HTML5大前端'
//   },
//   {
//     subjectId: 6,
//     subjectName: '大数据'
//   }
// ]
Mock.Random.extend({
  // subjectName: function(date) {
  //   const subjects = subJectArr
  //   return this.pick(...subjects)
  // },
  // image: function(date) {
  //   const images = [
  //     'https://t7.baidu.com/it/u=1819248061,230866778&fm=193&f=GIF',
  //     'https://t7.baidu.com/it/u=810585695,3039658333&fm=193&f=GIF',
  //     'https://t7.baidu.com/it/u=2869268957,1721811479&fm=193&f=GIF'
  //   ]
  //   return this.pick(images)
  // }
})
// Mock.mock('@SUBJECTNAME')
// Mock.mock('@IMAGE')
// const subjectid =
const listData = Mock.mock({
  'list|77': [
    {
      'id|+1': 1,
      description: '@ctitle(6)',
      wordName: '@title(1)',
      'subject|1': ['HTML5大前端', 'JavaEE+分布式', 'Python+人工智能', '全链路UI设计', '云计算', '大数据'],
      'wordHot|10-45': 1,
      name: '@cname(4)',
      createTime: '@date(yyyy-MM-dd HH:mm:ss)'
    }
  ]
})
// console.log(listData)
module.exports = [
  // 列表，查
  {
    url: '/mock/content/word',
    type: 'get',
    response: (config) => {
      if (config.query.id) {
        const res = listData.list.find(item => item.id * 1 === config.query.id * 1)
        if (res) {
          return {
            code: 0, message: '', data: res }
        } else {
          return { code: 9997, message: '数据不存在', data: res }
        }
      } else {
        let { page = 1, limit = 10 } = config.query
        const { wordName, description, name, subject } = config.query
        page = page * 1
        limit = limit * 1
        let clist = JSON.parse(JSON.stringify(listData.list))
        if (wordName) {
          console.log('wordName')
          clist = clist.filter(item => item.wordName === wordName)
          total = clist.length
        }
        if (description) {
          console.log('description')
          clist = clist.filter(item => item.description === description)
          total = clist.length
        }
        if (name) {
          console.log('name')
          clist = clist.filter(item => item.name === name)
          total = clist.length
        }
        if (subject) {
          clist = clist.filter(item => item.subject === subject.replace(' ', '+'))
          total = clist.length
        }
        let total = clist.length
        const list = clist.splice((page - 1) * limit, limit)
        return {
          code: 0,
          message: 'ok',
          data: {
            total,
            list
          }
        }
      }
    }
  },

  // 增
  {
    url: '/mock/content/word',
    type: 'post',
    response: (config) => {
      const { body } = config
      // if (!body.tagname) {
      //   return { code: 9997, message: '必填项不能是空的', data: '' }
      // }
      body.id = listData.list[listData.list.length - 1].id + 1
      body.createTime = new Date().toLocaleString().replace(/\//g, '-')
      listData.list.push(body)
      return { code: 0, message: '成功', data: '' }
    }
  },
  // 改
  {
    url: '/mock/content/word',
    type: 'put',
    response: (config) => {
      // console.log(config)
      const { body } = config
      if (!body.id) {
        return { code: 9997, message: '请提交ID', data: '' }
      }
      const index = listData.list.findIndex(
        item => item.id * 1 === body.id * 1
      )
      if (index === -1) {
        return { code: 9997, message: '无数据', data: '' }
      }
      listData.list[index] = body
      return { code: 0, message: '成功', data: '' }
    }
  },
  // 删
  {
    url: '/mock/content/word',
    type: 'delete',
    response: (config) => {
      if (!config.query.id) {
        return { code: 9997, message: '请提交ID', data: '' }
      }
      const index = listData.list.findIndex(
        item => item.id * 1 === config.query.id * 1
      )
      if (index === -1) {
        return { code: 9997, message: '无数据', data: '' }
      }
      listData.list.splice(index, 1)
      return { code: 0, message: '成功', data: '' }
    }
  }
]
